package com.sr.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sr.entity.Author;
import com.sr.service.AuthorService;

/**
 * Servlet implementation class Authorlogin
 */
@WebServlet("/login")
public class AuthorLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public AuthorLoginServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		Author author=new Author(username,password);
		System.out.println(author);
		AuthorService as=new AuthorService();
		boolean result=as.getByName(username, password);
		if(result){
			
			author=as.findUserName(username);
			request.getSession().setAttribute("author", author);
			request.getRequestDispatcher("authorindex.jsp").forward(request, response);
			
		}else{
			System.out.println("登录失败！");
			response.sendRedirect("authorlogin.jsp");
		}
		
	}

}
